//
//  CFIle.m
//  jumpingfrog
//
//  Created by Tri Vo on 8/28/13.
//  Copyright (c) 2013 Tri Vo. All rights reserved.
//

#import "CFIle.h"

@implementation CFIle

+ (NSArray *)readFile:(NSString *)fileName {
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"txt"];
    NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
    NSArray *lines = [contents componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\r\n"]];
//    for (NSString* line in lines) {
//        if (line.length) {
//            NSLog(@"line: %@", line);
//        }
//    }
    return lines;
}

+ (NSString *)readFileInC:(FILE *)f {
    char buffer[4096];
    
    // tune this capacity to your liking -- larger buffer sizes will be faster, but
    // use more memory
    NSMutableString *result = [NSMutableString stringWithCapacity:256];
    
    // Read up to 4095 non-newline characters, then read and discard the newline
    int charsRead;
    do
    {
        if(fscanf(f, "%4095[^\n]%n%*c", buffer, &charsRead) == 1)
            [result appendFormat:@"%s", buffer];
        else
            break;
    } while(charsRead == 4095);
    
    return result;
}

@end
